﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Model.Interfaces;
using Model.ModelEventArgs;
using Model.ModelClasses;

namespace View.Students
{
    public partial class MainStudentsPanel
    {
        public event EventHandler<StudentEventArgs> AddNewStudentEvent;
        public event EventHandler InputAddressEvent;
        public event EventHandler<StudentEventArgs> EditStudentEvent;

//        private string []_filterAttributes = {"ФИО"};

//        private List<IStudent> _students;
        private StudentPanel studentPanel;
        public Student currentStudent;

        public MainStudentsPanel()
        {
            InitializeComponent();
           /* var iter = Data.DataDispatcher.GetInstance().Students.GetEnumerator();
            iter.MoveNext();*/
            studentPanel = new StudentPanel(null);
            studentPanel.IsEnabled = false;

           // studentPanel.IsEnabled = false;
            //studentPanel.InputAddressEvent += InputAddressEventHandler;

            StudentsDataGrid.SelectedIndex = 0;
            StudentsDataGrid.ItemsSource = Data.DataDispatcher.GetInstance().Students;
            //studentPanelGrid.Children.Add(studentPanel);      remove because of new addition way


        }

        private void AddNewStudentButtonClick(object sender, RoutedEventArgs e)
        {
            if (AddNewStudentEvent != null)
            {
                currentStudent = new Student();
                AddNewStudentEvent(this, new StudentEventArgs(currentStudent));
            }
        }

        /*protected void InputAddressEventHandler(object sender, EventArgs evArgs)
        { if (InputAddressEvent != null) InputAddressEvent(this, new EventArgs()); }*/

        private void FiterTextBoxTextChanged(object sender, TextChangedEventArgs e)
        {
            if (FilteringAttributeComboBox.SelectedValue == null) return;
            switch (FilteringAttributeComboBox.SelectedIndex)
            {
                case 0: StudentsDataGrid.Items.Filter += FullNameFilterPredicate;
                    break;

                default: MessageBox.Show(FilteringAttributeComboBox.SelectedValue.ToString());
                    break;
            }
        }

        private bool FullNameFilterPredicate(object obj)
        {
            return (((IStudent) obj).FullName).Contains(FiterTextBox.Text);
        }

        /*private void StudentsDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (StudentsDataGrid.SelectedIndex == -1)
            {
                studentPanel.DataContext = null;
                studentPanel.IsEnabled = false;
            }
            else
            {
                studentPanel.DataContext = StudentsDataGrid.CurrentItem;
                studentPanel.IsEnabled = true;
            }
         * 
         * SelectionChanged="StudentsDataGrid_SelectionChanged"
         * 
        }*/

        private void EditStudentButtonClick(object sender, RoutedEventArgs e)
        {
            if (EditStudentEvent != null)
            {
                currentStudent = (Student)(StudentsDataGrid.SelectedItem);
                if(currentStudent != null)
                {
                    EditStudentEvent(this, new StudentEventArgs(currentStudent));
                }
            }
        }
    }
}